from django.db import models
from django.contrib.auth.models import User


class ExchangeItem(models.Model):
    """交换物品表 - 用户发布的可交换物品信息"""
    CITY_CHOICES = [
        ('北京', '北京'), ('上海', '上海'), ('广州', '广州'), ('深圳', '深圳'),
        ('杭州', '杭州'), ('南京', '南京'), ('武汉', '武汉'), ('成都', '成都'),
        ('重庆', '重庆'), ('西安', '西安'), ('天津', '天津'), ('苏州', '苏州'),
        ('其他', '其他')
    ]
    CATEGORY_CHOICES = [
        ('服饰', '服饰'), ('电子', '电子'), ('书籍', '书籍'), ('家居', '家居'),
        ('运动', '运动'), ('玩具', '玩具'), ('其他', '其他')
    ]
    STATUS_CHOICES = [
        ('available', '可交换'),
        ('exchanged', '已交换'),
    ]
    OFFER_TYPE_CHOICES = [
        ('exchange', '可交换'),
        ('gift', '可赠送'),
    ]

    item_id = models.AutoField(primary_key=True, verbose_name='物品唯一ID', db_comment='物品唯一ID')
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE, db_column='user_id', verbose_name='发布者用户', db_comment='发布者用户', null=True, blank=True)
    title = models.CharField(max_length=255, verbose_name='AI生成的标题', db_comment='AI生成的标题')
    description = models.TextField(null=True, blank=True, verbose_name='AI生成的描述', db_comment='AI生成的描述')
    image_url = models.CharField(max_length=255, null=True, blank=True, verbose_name='物品图片URL', db_comment='物品图片URL')
    category = models.CharField(max_length=100, null=True, blank=True, choices=CATEGORY_CHOICES, verbose_name='AI识别分类', db_comment='AI识别分类')
    city = models.CharField(max_length=50, null=True, blank=True, choices=CITY_CHOICES, verbose_name='城市', db_comment='发布所在城市')
    lat = models.FloatField(null=True, blank=True, verbose_name='纬度', db_comment='发布位置纬度')
    lng = models.FloatField(null=True, blank=True, verbose_name='经度', db_comment='发布位置经度')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='available', verbose_name='物品状态', db_comment='物品状态')
    offer_type = models.CharField(max_length=20, choices=OFFER_TYPE_CHOICES, default='exchange', null=True, blank=True, verbose_name='供给类型', db_comment='交换或赠送')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间', db_comment='发布时间')

    class Meta:
        db_table = 'xiangsu_exchange_items'
        db_table_comment = '交换物品表-用户发布的可交换物品信息'
        verbose_name = '交换物品'
        verbose_name_plural = '交换物品'
        ordering = ['-created_at']

    def __str__(self):
        return str(self.title)


class ExchangeTransaction(models.Model):
    """交换记录表 - 物品交换的交易记录"""
    transaction_id = models.AutoField(primary_key=True, verbose_name='记录唯一ID', db_comment='记录唯一ID')
    item = models.ForeignKey('ExchangeItem', on_delete=models.CASCADE, db_column='item_id', verbose_name='交换物品ID', db_comment='交换物品ID')
    provider = models.ForeignKey('auth.User', on_delete=models.CASCADE, db_column='provider_user_id', verbose_name='提供者', db_comment='提供者', related_name='exchange_provider', null=True, blank=True)
    receiver = models.ForeignKey('auth.User', on_delete=models.CASCADE, db_column='receiver_user_id', verbose_name='接收者', db_comment='接收者', related_name='exchange_receiver', null=True, blank=True)
    completed_at = models.DateTimeField(auto_now_add=True, verbose_name='完成时间', db_comment='完成时间')

    class Meta:
        db_table = 'xiangsu_exchange_transactions'
        db_table_comment = '交换记录表-物品交换的交易记录'
        verbose_name = '交换记录'
        verbose_name_plural = '交换记录'

    def __str__(self):
        return f'交易{self.transaction_id}'
